/*
 * @Author: panzhiwei 749458928@qq.com
 * @Date: 2024-01-19 14:54:39
 * @LastEditors: panzhiwei 749458928@qq.com
 * @LastEditTime: 2024-01-26 16:34:20
 * @FilePath: /2DDemo/assets/Script/EnemySpawner.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, Prefab, instantiate, Vec3, view, math } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnemySpawner')
export class EnemySpawner extends Component {
    @property({ type: Prefab })
    public enemyPrefab: Prefab | null = null;

    @property
    public spawnInterval: number = 100; // 敌人生成间隔（秒）

    @property
    public enemiesPerSpawn: number = 1; // 每次生成的敌人数量

    @property
    public health: number = 100; // 敌人的生命值

    private spawnTimer: number = 0;

    update(deltaTime: number) {
        // 更新计时器
        this.spawnTimer += deltaTime;
        if (this.spawnTimer >= this.spawnInterval) {
            this.spawnTimer = 0;
            for (let i = 0; i < this.enemiesPerSpawn; i++) {
                this.spawnEnemy();
            }
        }

        // 更新敌人位置
        // this.updateEnemyPosition(deltaTime);
    }

    spawnEnemy() {
        if (this.enemyPrefab) {
            // 实例化怪物
            const enemy = instantiate(this.enemyPrefab);

            // 获取视图大小
            const winSize = view.getVisibleSize();

            // 设置怪物的初始位置
            const x = math.randomRange(-winSize.width / 2, winSize.width / 2);
            const y = winSize.height / 2;
            enemy.setPosition(new Vec3(x, y, 0));

            // 将怪物添加到场景中
            this.node.addChild(enemy);
        }
    }

}
